

$(document).ready(function(){

    function stateFormatter(value, row, index) {
        if (row.id === 2) {
            return {
                disabled: true,
                checked: true
            };
        }
        if (row.uid === 0) {
            return {
                disabled: true,
                checked: true
            }
        }
        return value;
    }

    function columns(){
        return [
            {
                field: 'state',
                checkbox: true,
                formatter:stateFormatter
            },{
                field:'uid',
                title:'uid'
            },{
                field:'username',
                title:'用户名'
            },{
                field:'email',
                title:'邮箱'
            },{
                field:'class',
                title:'组织信息'
            }];
    }

    function queryParams(para){
        var querydata = {
            searchkey:"123"
        };

        return querydata;
    }

    function onClickRow(row){
        $("#edit-uid").html(row.uid);
        $('#edit-username').val(row.username);
        $("#edit-useremail").val(row.email);
        $("#edit-user").modal('show');

        $("#edit-user-save").click(function(){
            var username;
            var useremail;
            ($("#edit-username").val() == row.username)?username='':username = $("#edit-username").val();
            ($("#edit-useremail").val() == row.email)?useremail='':useremail = $("#edit-useremail").val();
            $.ajax({
                type:"POST",
                url:"/Home/Orgnizer/updateUserInfo",
                dataType:"json",
                async : false,
                data:{
                    uid:row.uid,
                    username:username,
                    useremail:useremail,
                    userpassword:$("#edit-userpassword").val()
                },
                success:function(data){
                    alert(data.msg);

                },error: function (jqXHR) {
                    alert("failed"+jqXHR.status);
                }
            });
        });
    }

    var tabletool = new TableTools();
    tabletool.bstable("user-list-table", "/Home/Admin/getAllUserList", columns(), queryParams, onClickRow);

    $("#user-delete").click(function(){

        $.ajax({
            type:"POST",
            url:"__URL__/deleteUsers",
            dataType:"json",
            data:{
                userlist:tabletool.return_userlist_jsondata()
            },
            success:function(data){
                alert(data.msg);

            },error: function (jqXHR) {
                alert("failed"+jqXHR.status);
            }
        });

    });

    $("#add-user-btn").click(function(){
        $("#add-user").modal('show');
    });

});